package com.icen.utils.comm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

/**
 * System Utils Class
 * 
 * @author IcenHan
 * 
 */
public class SystemUtils {

	/**
	 * 当前屏幕是否为竖屏
	 * 
	 * @param context
	 * @return false
	 */
	public static boolean isScreenOriatationPortrait(Context context) {
		return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;

	}

	/**
	 * 打电话
	 * 
	 * @param context
	 * @param number
	 *            电话号码
	 */
	public static void callPhone(Context context, String number) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ number));
		context.startActivity(intent);
	}

	/**
	 * 跳转到打电话界面
	 * 
	 * @param context
	 * @param number
	 *            电话号码
	 */
	public static void goPhoneUi(Context context, String number) {
		Uri uri = Uri.parse("tel:" + number);
		Intent intent = new Intent(Intent.ACTION_DIAL, uri);
		context.startActivity(intent);
	}

	/**
	 * 直接发短信
	 * 
	 * @param context
	 * @param number
	 * @param smsBody
	 */
	public static void sendSms(Context context, String number, String smsBody) {
		Uri smsToUri = Uri.parse("smsto:" + number);
		Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
		intent.putExtra("sms_body", smsBody);
		context.startActivity(intent);
	}

	/**
	 * 进入 发短信界面
	 * 
	 * @param context
	 * @param number
	 */
	public static void intoSms(Context context, String number) {
		Uri smsToUri = Uri.parse("smsto:" + number);
		Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
		intent.putExtra("sms_body", "");
		context.startActivity(intent);
	}

	/**
	 * 返回 桌面
	 * 
	 * @param context
	 */
	public static void goHome(Context context) {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addCategory(Intent.CATEGORY_HOME);
		context.startActivity(intent);
	}

	/**
	 * 判断SdCard是否可用
	 * 
	 * @return boolean
	 */
	public static boolean isCanUseSdCard() {
		try {
			return Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取SdCard的路径
	 * 
	 * @return String
	 *         <p>
	 *         null:表示SdCard不可用
	 *         </p>
	 */
	public static String getSdCardPath() {
		String path = null;
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			path = Environment.getExternalStorageDirectory().toString();
		}
		return path;
	}

	/**
	 * 获取SdCard可用空间
	 * 
	 * @param context
	 * @return String
	 */
	public static String getAvailableSdRom(Context context) {
		if (!isCanUseSdCard())
			return "0";
		File path = Environment.getExternalStorageDirectory();
		StatFs stat = new StatFs(path.getPath());
		@SuppressWarnings("deprecation")
		long blockSize = stat.getBlockSize();
		@SuppressWarnings("deprecation")
		long availableBlocks = stat.getAvailableBlocks();
		return Formatter.formatFileSize(context, availableBlocks * blockSize);
	}

	/**
	 * 判断系统是否锁屏
	 * 
	 * @param context
	 * @return boolean
	 */
	public static boolean isLockScreen(Context context) {
		KeyguardManager keyguardManager = (KeyguardManager) context
				.getSystemService(Context.KEYGUARD_SERVICE);
		return keyguardManager.inKeyguardRestrictedInputMode();
	}

	/**
	 * 锁屏并设置密码
	 * 
	 * @param lockScreenPassword
	 */
	public static void setLockScreen(Context context, String lockScreenPassword) {
		DevicePolicyManager manager = (DevicePolicyManager) context
				.getSystemService(Context.DEVICE_POLICY_SERVICE);
		manager.resetPassword(lockScreenPassword, 0);
		manager.lockNow();
	}

	/**
	 * 获取可用的手机内空间Rom
	 * 
	 * @param context
	 * @return String
	 */
	public static String getAvailablePhoneRom(Context context) {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		@SuppressWarnings("deprecation")
		long blockSize = stat.getBlockSize();
		@SuppressWarnings("deprecation")
		long availableBlocks = stat.getAvailableBlocks();
		return Formatter.formatFileSize(context, (availableBlocks * blockSize));
	}

	/**
	 * 获取可用的闪存Ram
	 * 
	 * @param context
	 * @return String
	 */
	public static String getAvailableRam(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		MemoryInfo mi = new MemoryInfo();
		am.getMemoryInfo(mi);
		return Formatter.formatFileSize(context, mi.availMem);
	}

	/**
	 * 获取总闪存空间Ram
	 * 
	 * @return String
	 */
	public static String getTotalRam(Context context) {
		String fileName = "/proc/meminfo"; // 系统内存信息文件
		String info;
		long totalMemory = 0;
		try {
			FileReader localFileReader = new FileReader(fileName);
			BufferedReader localBufferedReader = new BufferedReader(
					localFileReader, 8192);
			info = localBufferedReader.readLine();

			info = info.replaceAll(" ", "");
			info = info.replace("kB", "");
			info = info.replace("MemTotal:", "");
			totalMemory = Integer.valueOf(info).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte
			localBufferedReader.close();

		} catch (IOException e) {
		}
		return Formatter.formatFileSize(context, totalMemory);// Byte转换为KB或者MB，内存大小规格化
	}

}
